﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.db;
using ForkCarRelationManager.Business;
using ForkCarRelationManager.Utility;
using common;
using ForkCarRelationManager.DB;
namespace ForkCarRelationManager.Controls
{
    public partial class RemindControl : UserControl
    {
        Util util = new Util();
        DataPaging RemindPage;
        DataSet RemindDataSet;
        int pagecount = 100000;//每页数据行数
      
        RemindBI remindBI ;
        RepireBI repireBI = new RepireBI();
        DataPaging RepirePage;
        DataSet RepireDataSet;
        dbOperate dbo = new dbOperate();
        string datetime;
        public RemindControl()
        {
            InitializeComponent();
        }

        private void RemindControl_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.White;
            datetime = DateTime.Now.ToString("yyyy/M/d");
            remindBI = new RemindBI(datetime, pagecount);
            RemindPage = remindBI.GetRemindpage();
            RepirePage = repireBI.GetRepirepage(pagecount);
            remindGridView.ReadOnly = true;
            RepireGridView.ReadOnly = true;
            initControl();
            
        }
        private void initControl()
        {

            RemindDataSet = RemindPage.Homepage();
            remindGridView.DataSource = RemindDataSet;
            remindGridView.DataMember = "data";
            initRepire();
        /*    if (remindBI.GetDatacount() == 0)
            {
                //Nonetext.Show();
              //  remindGridView.Hide();
                RepirePanel.Hide();
             
            }
            else
            {
               // Nonetext.Hide();
               // remindGridView.Show();
                RepirePanel.Show();
               
            }*/
    
           
            
        }
        private void initRepire()
        {
            RepireDataSet = RepirePage.Homepage();
            RepireGridView.DataSource = RepireDataSet;
            RepireGridView.DataMember = "data";
        }
    

        private void remindGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (remindGridView.CurrentRow!=null&e.ColumnIndex>=0)
            {
                string id = remindGridView.CurrentRow.Cells["Column11"].Value.ToString();
                if (remindGridView.Columns[e.ColumnIndex].Name == "Column9")
                {
                    if (remindBI.checkRemindInfor(id))
                    {
                        remindGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "已处理";
                    }
                    else
                        MessageBox.Show("处理失败");
                 /*   if (remindBI.DeleteRemindInfor(id))
                    {
                        initControl();
                    }
                    else
                    {
                        MessageBox.Show("处理失败");
                    }*/

                }
                if (remindGridView.Columns[e.ColumnIndex].Name == "Column10")
                {

                    if (remindBI.UpdateRemindInfor(id))
                    {
                        initControl();
                    }
                    else
                        MessageBox.Show("延时失败");
                }
                if (remindGridView.Columns[e.ColumnIndex].Name == "Column26")
                {
                    int maintenID = remindBI.getMaintenidbyid(Convert.ToInt32(id));
                    MaintenInforForm maintenInfor = new MaintenInforForm(maintenID);
                 //   RemindDetailForm reminddetailform = new RemindDetailForm(Convert.ToInt32(id));
                    maintenInfor.ShowDialog();
                    if (maintenInfor.DialogResult == DialogResult.Yes)
                    {
                        this.initControl();
                    }
                }
            }
        }
        //添加客户
        private void button1_Click(object sender, EventArgs e)
        {
            FormAddClient formAddClient = new FormAddClient();
            formAddClient.ShowDialog();
        }

        //添加维修记录
        private void button2_Click(object sender, EventArgs e)
        {
            FormAddMainten formAddMainten = new FormAddMainten();
            formAddMainten.ShowDialog();
            if (formAddMainten.DialogResult == DialogResult.Yes)
            {
                this.initControl();
            }
        }

        //一键处理
        private void button3_Click(object sender, EventArgs e)
        {
           DataTable alldata = remindBI.findTodayRemind();
           if (alldata.Rows.Count > 0)
           {
               for (int i = 0; i < alldata.Rows.Count; i++)
               {
                   String id = alldata.Rows[i]["ID"].ToString();
                   remindBI.checkRemindInfor(id);
               }
               initControl();
           }
           else
           {
               MessageBox.Show("没有数据可以处理！");
           }
        }

        //导出excel
        private void button5_Click(object sender, EventArgs e)
        {
            if (util.Ifpower(LoginInfo.CurrentUser.AccountName) == 1)
            {
                ExportExl exportExl = new ExportExl();
                exportExl.ExportToExcel(this.remindGridView, RemindPage.allData(), 2);
            }
            else
                MessageBox.Show("您没有导出excel的权限!");
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            AddRepireForm addRepireForm = new AddRepireForm();
            addRepireForm.ShowDialog();
            if (addRepireForm.DialogResult == DialogResult.Yes)
            {
                this.initRepire();
            }
        }

        private void RepireGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (RepireGridView.CurrentRow != null&&e.ColumnIndex>=0)
            {
                if (RepireGridView.Columns[e.ColumnIndex].Name == "Column23")
                {
                    string id = RepireGridView.CurrentRow.Cells[(RepireGridView.CurrentRow.Cells.Count) - 1].Value.ToString();
                    GiveupForm giveup = new GiveupForm(Convert.ToInt32(id));
                    giveup.ShowDialog();
                    if (giveup.DialogResult == DialogResult.Yes)
                    {
                        this.initRepire();
                    }
                }
                else if (RepireGridView.Columns[e.ColumnIndex].Name == "Column22")
                {
                    int repireID =Convert.ToInt32( RepireGridView.CurrentRow.Cells[(RepireGridView.CurrentRow.Cells.Count) - 1].Value.ToString());
                    int clientID =Convert.ToInt32( RepireGridView.CurrentRow.Cells[(RepireGridView.CurrentRow.Cells.Count) - 2].Value.ToString());
                    string clientName=RepireGridView.CurrentRow.Cells["Column13"].Value.ToString();
                    FormAddMainten addmaintenform = new FormAddMainten(repireID, clientID, clientName);
                    addmaintenform.ShowDialog();
                    if (addmaintenform.DialogResult == DialogResult.Yes)
                    {
                        this.initRepire();
                    }
                }
                else if (RepireGridView.Columns[e.ColumnIndex].Name == "查看")
                {
                    int Ifhandle = Convert.ToInt32(RepireGridView.CurrentRow.Cells["Column25"].Value.ToString());
                    int repireID = Convert.ToInt32(RepireGridView.CurrentRow.Cells[(RepireGridView.CurrentRow.Cells.Count) - 1].Value.ToString());
                    RepireDetailForm RepireForm = new RepireDetailForm(repireID, Ifhandle);
                    RepireForm.ShowDialog();
                    if (RepireForm.DialogResult == DialogResult.Yes)
                    {
                        this.initControl();
                    }
                }
                
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (util.Ifpower(LoginInfo.CurrentUser.AccountName) == 1)
            {
                ExportExl exportExl = new ExportExl();
                exportExl.ExportToExcel(this.RepireGridView, RepirePage.allData(), 2);
            }
            else
                MessageBox.Show("您没有导出excel的权限!");
          
        }

      

    }
}
